<template>
	<view>
		<Navbar></Navbar>
		
		<Modal :show="modal.show" :confirmText="modal.confirm" :modalContent="modal.content" :showCancel="modal.showCancel"
			@confirm="modalConfirm" @cancel="modalCancel"></Modal>
	</view>
</template>

<script>
	import Navbar from '@/components/Navbar.vue'
	import Modal from '@/components/Modal.vue'
	import { userLogin, bindUserId } from '@/network/api.js'
	import { teaSign, teaDetail } from '@/network/tea.js'
	import { InitScene } from '@/utils/index.js'
	export default{
		components:{
			Navbar,
			Modal
		},
		data(){
			return{
				modal: {
					show: false,
					showCancel: false,
					confirm: '确定',
					content: ''
				},
				teaId: 0
			}
		},
		async onLoad(options){
			await this.$onLaunched
			this.teaId = InitScene(options.scene).id
			this.getUserCode(this.teaId)
			const pages = getCurrentPages()
			const currentPage = pages[pages.length - 1]
			const params = {
				userid: uni.getStorageSync("user_id"),
				inviter: InitScene(options.scene).inviterId,
				url: currentPage.$page.fullPath || ''
			}
			bindUserId(params)
		},
		methods:{
			getUserCode(id){
				const _this = this
				uni.login({
					provider: "weixin",
					success(res){
						userLogin({code:res.code}).then(res=>{
							let status = res.data.user_data.status
							if([0,'0'].includes(status)){
								_this.modal.content = '部分功能需注册解锁，注册后即可体验完整服务'
								_this.modal.confirm = '前往注册'
								_this.modal.showCancel = true
								_this.modal.show = true
							}else{
								const params = {
									tea_id: id,
									user_id: res.data.userid
								}
								teaSign(params).then(res=>{
									_this.modal.content = `${res.msg}`
									_this.modal.confirm = '确定'
									_this.modal.showCancel = false
									_this.modal.show = true
								})
							}
						})
					}
				})
			},
			modalConfirm(){
				if(this.modal.showCancel){
					const params = {
						user_id: uni.getStorageSync("user_id"),
						tea_id: this.teaId
					}
					teaDetail(params).then(res => {
						uni.setStorageSync("inviterUrl", `teaBureau/teaDetail/teaDetail?id=${this.teaId}&inviterId=${res.data.user.id}`)
						uni.navigateTo({
							url: "/pages/register/register"
						})
					})
				}else{
					uni.redirectTo({
						url: `/teaBureau/teaDetail/teaDetail?id=${this.teaId}`
					})
				}
			},
			modalCancel(){
				uni.switchTab({
					url: "/pages/home/home"
				})
			}
		}
	}
</script>

<style>
</style>